/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

package org.erossi.pigest.web.iva;

import java.util.Calendar;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import junit.framework.TestCase;

import org.erossi.common.util.AppCtx;
import org.erossi.pigest.model.iva.AliquotaIva;
import org.erossi.pigest.model.iva.AliquotaIvaBus;
import org.hibernate.ObjectNotFoundException;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.core.io.FileSystemResourceLoader;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.mock.web.MockHttpSession;
import org.springframework.mock.web.MockServletContext;
import org.springframework.validation.BindException;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.context.support.XmlWebApplicationContext;
import org.springframework.web.servlet.ModelAndView;

/**
 * 'InserisciAliquotaIvaControllerTest' class: test unitari per l'OUT 
 *   {@link org.erossi.pigest.web.iva.InserisciAliquotaIvaController}
 *
 * @author erossi
 * @version Sep 6, 2009
 */
public class InserisciAliquotaIvaControllerTest extends TestCase {

  private static final Double TEST_ALIQUOTA = 20.0D;

  private static final Long TEST_ALIQUOTA_IVA_ID = 1000L;

  private String timestamp;

  private AliquotaIva aliquotaIva;
  
  private AliquotaIvaBus aliquotaIvaBus;
  
  private InserisciAliquotaIvaController controller;
  
  private MockHttpServletRequest mockHttpServletRequest;
  
  private MockHttpServletResponse mockHttpServletResponse;
  
  private XmlWebApplicationContext apc;

  /**
   * Crea una nuova istanza della classe
   */
  public InserisciAliquotaIvaControllerTest() {
    super();

    timestamp = null;
    mockHttpServletRequest = null;
    mockHttpServletResponse = null;

    aliquotaIva = null;
    aliquotaIvaBus = null;
    controller = null;

    apc = null;
  }

  /**
   * Prepara l'ambiente per l'esecuzione del test.
   * 
   * @throws java.lang.Exception eccezioni non gestite
   */
  @Before
  public void setUp() throws Exception {
    
    MockServletContext mockServletContext = new MockServletContext(".", new FileSystemResourceLoader());
    MockHttpSession mockHttpSession = new MockHttpSession(mockServletContext);
    mockHttpServletRequest = new MockHttpServletRequest(mockServletContext, "GET", "/inserisciAliquotaIva.htm");
    mockHttpServletRequest.setSession(mockHttpSession);
    
    ServletRequestAttributes sra = new ServletRequestAttributes(mockHttpServletRequest);
    RequestContextHolder.setRequestAttributes(sra);
    
    apc = new XmlWebApplicationContext();
    apc.setServletContext(mockServletContext);
    apc.setConfigLocations(new String[] {"web/WEB-INF/spring-servlet.xml"});
    apc.refresh();

    timestamp = String.format("%1$tM%1$tS%1$tL", Calendar.getInstance());

    aliquotaIva = (AliquotaIva) apc.getBean("aliquotaIva");
    aliquotaIva.setDescrizione("DESCRIZIONE_" + timestamp);
    aliquotaIva.setAliquota(InserisciAliquotaIvaControllerTest.TEST_ALIQUOTA);
    
    aliquotaIvaBus = (AliquotaIvaBus) apc.getBean("aliquotaIvaBus");
    
    controller = (InserisciAliquotaIvaController) apc.getBean("inserisciAliquotaIvaController"); 
  }
  
  /**
   * Metodo di test per {@link org.erossi.pigest.web.iva.InserisciAliquotaIvaController#formBackingObject(HttpServletRequest)}.
   */
  @Test
  public void testFormBackingObjectInsert() throws Exception {
    
    Object response = controller.formBackingObject(mockHttpServletRequest);
    assertNotNull(response);
    assertTrue(response instanceof AliquotaIva);
    assertNull(((AliquotaIva) response).getId());
     
  }
  
  /**
   * Metodo di test per {@link org.erossi.pigest.web.iva.InserisciAliquotaIvaController#formBackingObject(HttpServletRequest)}.
   */
  @Test
  public void testFormBackingObjectModifyCorrect() throws Exception {
    
    AliquotaIva inserito = aliquotaIvaBus.registra(aliquotaIva);
    assertNotNull(inserito);
    assertNotNull(inserito.getId());  
    
    mockHttpServletRequest.setParameter(InserisciAliquotaIvaController.ID_ALIQUOTA_IVA, String.valueOf(inserito.getId()));
    
    Object response = controller.formBackingObject(mockHttpServletRequest);
    assertNotNull(response);
    assertTrue(response instanceof AliquotaIva);
    assertTrue(inserito.equals(response));
    
  }
  
  /**
   * Metodo di test per {@link org.erossi.pigest.web.iva.InserisciAliquotaIvaController#formBackingObject(HttpServletRequest)}.
   */
  @Test
  public void testFormBackingObjectModifyNoId() throws Exception {
    
    AliquotaIva inserito = aliquotaIvaBus.registra(aliquotaIva);
    assertNotNull(inserito);
    assertNotNull(inserito.getId()); 
    
    mockHttpServletRequest.setParameter(InserisciAliquotaIvaController.ID_ALIQUOTA_IVA, "");
    
    try {
      
      Object response = controller.formBackingObject(mockHttpServletRequest);
      fail("Recuperato oggetto non esistente/non corretto: " + AppCtx.printDebug("response", response));
      
    } catch (NumberFormatException e) {
      
      assertNotNull(e);
      
    } catch (Exception e) {
      
      fail("Eccezione: " + AppCtx.printDebug("e", e));
      
    }
    
  }

  /**
   * Metodo di test per {@link org.erossi.pigest.web.iva.InserisciAliquotaIvaController#formBackingObject(HttpServletRequest)}.
   */
  @Test
  public void testFormBackingObjectModifyNonExistentId() throws Exception {
    
    AliquotaIva inserito = aliquotaIvaBus.registra(aliquotaIva);
    assertNotNull(inserito);
    assertNotNull(inserito.getId()); 
    
    mockHttpServletRequest.setParameter(InserisciAliquotaIvaController.ID_ALIQUOTA_IVA, String.valueOf(InserisciAliquotaIvaControllerTest.TEST_ALIQUOTA_IVA_ID));
    
    try {
    
      Object response = controller.formBackingObject(mockHttpServletRequest);
      fail("Recuperato oggetto non esistente/non corretto: " + AppCtx.printDebug("response", response));
      
    } catch (ObjectNotFoundException e) {
      
      assertNotNull(e);
      
    } catch (Exception e) {
      
      fail("Eccezione: " + AppCtx.printDebug("e", e));
      
    }
    
  }
  
  /**
   * Metodo di test per {@link org.erossi.pigest.web.iva.InserisciAliquotaIvaController#formBackingObject(HttpServletRequest)}.
   */
  @Test
  public void testFormBackingObjectModifyNonLongId() throws Exception {
    
    AliquotaIva inserito = aliquotaIvaBus.registra(aliquotaIva);
    assertNotNull(inserito);
    assertNotNull(inserito.getId()); 
    
    mockHttpServletRequest.setParameter(InserisciAliquotaIvaController.ID_ALIQUOTA_IVA, "X");
    
    try {
    
      Object response = controller.formBackingObject(mockHttpServletRequest);
      fail("Recuperato oggetto non esistente/non corretto: " + AppCtx.printDebug("response", response));
      
    } catch (NumberFormatException e) {
      
      assertNotNull(e);
      
    } catch (Exception e) {
      
      fail("Eccezione: " + AppCtx.printDebug("e", e));
      
    }
    
  }
  
  /**
   * Metodo di test per {@link org.erossi.pigest.web.iva.InserisciAliquotaIvaController#onSubmit(HttpServletRequest, HttpServletResponse, Object, BindException)}.
   */
  @Test
  public void testOnSubmit() throws Exception {

    mockHttpServletResponse = new MockHttpServletResponse();

    ModelAndView modelAndView = controller.onSubmit(mockHttpServletRequest, mockHttpServletResponse, aliquotaIva, null);
    assertNotNull(modelAndView);
    assertEquals(controller.getSuccessView(), modelAndView.getViewName());
    assertEquals(0, modelAndView.getModel().size());

    List<AliquotaIva> aliquotaIvaList = aliquotaIvaBus.cercaPerEsempio(aliquotaIva, new String[0]);
    assertNotNull(aliquotaIvaList);
    assertEquals(1, aliquotaIvaList.size());
    assertTrue(aliquotaIva.equals((AliquotaIva) aliquotaIvaList.get(0)));

  }
  
  /**
   * Ripulisce l'ambiente di test dopo l'esecuzione di ogni singolo metodo
   * 
   * @throws java.lang.Exception
   */
  @After
  public void tearDown() throws Exception {
  aliquotaIvaBus.cancella(aliquotaIva);
    apc.close();
  }

}

